package com.njtoyo.taxi.admin.rest.controller.platform.sensitive;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njtoyo.taxi.admin.aop.PermissionScope;
import com.njtoyo.taxi.admin.rest.presenter.platfrom.sensitive.SensitiveOperationPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.platform.sensitive.SensitiveOperationWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.platform.sensitive.SensitiveRegisteredWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.platform.sensitive.SensitiveUpdateWrapper;
import com.njtoyo.taxi.admin.service.platform.SensitiveOperationService;
import com.taxi.entity.common.RestResult;
import com.njtoyo.taxi.entity.backend.AdminUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import javax.validation.Valid;

@RestController
@RequestMapping("platform/sensitive-operation")
@Api(tags = "敏感操作管理")
public class SensitiveOperationController {

    @Autowired
    private SensitiveOperationService sensitiveOperationService;

    @PostMapping("/{operationName}/code")
    @ApiOperation(value = "发送授权码", notes = "发送指定授权的授权码")
    RestResult sendAuthorizationCode(@ModelAttribute("admin") AdminUser adminUser, @PathVariable("operationName") String operationName) throws MessagingException {
        return sensitiveOperationService.sendAuthorizationCode(adminUser, operationName);
    }

    @GetMapping("")
    @ApiOperation(value = "敏感操作列表", notes = "敏感操作列表,权限 :sensitive_operation")
    @PermissionScope(names = {"sensitive_operation"})
    RestResult<Page<SensitiveOperationPresenter>> sensitiveOperationList(@ModelAttribute("admin") AdminUser adminUser,
                                                                         @Valid SensitiveOperationWrapper wrapper) {
        return sensitiveOperationService.sensitiveOperationList(adminUser, wrapper);
    }

    @PostMapping("")
    @ApiOperation(value = "注册敏感操作", notes = "注册敏感操作,权限:sensitive_operation_create")
    @PermissionScope(names = {"sensitive_operation_create"})
    RestResult registered(@Valid @RequestBody SensitiveRegisteredWrapper wrapper) {
        return sensitiveOperationService.registered(wrapper);
    }


    @PatchMapping("/{id}")
    @ApiOperation(value = "修改敏感操作", notes = "修改敏感操作,权限:sensitive_operation_update")
    @PermissionScope(names = {"sensitive_operation_update"})
    RestResult update(@PathVariable("id") Long id, @Valid @RequestBody SensitiveUpdateWrapper wrapper) {
        return sensitiveOperationService.update(id, wrapper);
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除敏感操作", notes = "删除敏感操作,权限:sensitive_operation_delete")
    @PermissionScope(names = {"sensitive_operation_delete"})
    RestResult delete(@PathVariable("id") Long id) {
        return sensitiveOperationService.delete(id);
    }


}
